/**
 * 报表设计公司(2011)
 */
package com.reportgear.report.view.grid.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.abbp.report.model.Cell;
import net.abbp.report.model.ReportModel;
import net.abbp.report.view.GridView;

import com.reportgear.report.model.auxiliary.ColumnRow;
import com.reportgear.report.view.grid.selection.GridSelection;
import com.reportgear.report.view.grid.selection.Selection;

/**
 * 网格辅助类
 * 
 * @version 1.0 2011-3-18
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class GridUtils {
    public static double InValidDouble = -1.0001D;
    /**
     * 没有拖动
     */
    public static int DRAG_NONE = 0;
    /**
     * 拖动选择Cell
     */
    public static int DRAG_CELLSELECTION = 1;
    /**
     * 拖动右下角选择
     */
    public static int DRAG_CELLSELECTION_BOTTOMRIGHT_CORNER = 2;
    /**
     * 拖动修改宽度
     */
    public static int DRAG_WIDTH = 1;
    /**
     * 拖动选择列
     */
    public static int DRAG_SELECT_COLUMNS = 2;
    /**
     * 拖动修改高度
     */
    public static int DRAG_HEIGHT = 3;
    /**
     * 拖动选择行
     */
    public static int DRAG_SELECT_ROWS = 4;

    /**
     * 选择单元格
     * 
     * @param gridView 工作薄
     * @param column 列
     * @param row 行
     */
    public static void doSelectCell(GridView gridView, int column, int row) {
        ReportModel reportModel = gridView.getReportModel();
        GridSelection gridSel = gridView.getGridSelection();
        gridSel.setType(GridSelection.Type.CELL);

        Selection sel = gridSel.getEditSelection();
        Cell cell = reportModel.getCell(column, row);
        if (cell == Cell.EMPTY_CELL) {
            // 有可能被合并，查找合并的单元格
            Cell megerTo = reportModel.getMergedToCell(column, row);
            if (megerTo == Cell.EMPTY_CELL) {
                gridSel.setEditBounds(column, row, 1, 1);
                gridSel.setOnlySelection(column, row, 1, 1);
            } else {
                gridSel.setEditBounds(megerTo.getColumn(), megerTo.getRow(), megerTo.getColumnSpan(), megerTo
                        .getRowSpan());
                gridSel.setOnlySelection(megerTo.getColumn(), megerTo.getRow(), megerTo.getColumnSpan(), megerTo
                        .getRowSpan());
            }
        } else {
            gridSel.setEditBounds(cell.getColumn(), cell.getRow(), cell.getColumnSpan(), cell.getRowSpan());
            gridSel.setOnlySelection(cell.getColumn(), cell.getRow(), cell.getColumnSpan(), cell.getRowSpan());
        }

        gridView.fireGridSelectionChanged();
    }

    public static ColumnRow getAdjustLastColumnRowOfReportPane(GridView workSheet) {
        ReportModel report = workSheet.getReportModel();
        return ColumnRow.valueOf(Math.max(1, report.getColumnCount()), Math.max(1, report.getRowCount()));
    }

    /**
     * 获取网格选择行Index列表
     * 
     * @param gridSelection
     *            网格选择集
     * @return 行Index列表
     */
    public static List<Integer> getGridSelectionRowIndex(GridSelection gridSelection) {
        List<Integer> result = new ArrayList<Integer>();
        for (int i = 0; i < gridSelection.getCellSelectionCount(); i++) {
            Selection rect = gridSelection.getCellSelection(i);

            for (int j = 0; j < rect.getRowSpan(); j++) {
                int k = rect.getRow() + j;
                if (result.contains(k)) {
                    continue;
                }

                result.add(k);
            }
        }
        Collections.sort(result);

        return result;
    }

    /**
     * 获取网格选择列Index列表
     * 
     * @param gridSelection
     *            网格选择集
     * @return 列Index列表
     */
    public static List<Integer> getGridSelectionColumnIndex(GridSelection gridSelection) {
        List<Integer> result = new ArrayList<Integer>();
        for (int i = 0; i < gridSelection.getCellSelectionCount(); i++) {
            Selection rect = gridSelection.getCellSelection(i);
            for (int j = 0; j < rect.getColumSpan(); ++j) {
                int k = rect.getColumn() + j;
                if (result.contains(k)) {
                    continue;
                }

                result.add(k);
            }
        }
        Collections.sort(result);

        return result;
    }
}
